package com.qf.controller;

import com.qf.constants.Status;
import com.qf.dto.DeveloperDto;
import com.qf.model.Developer;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.DeveloperService;
import com.qf.vo.DevProVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("developer")
public class DeveloperController {
    @Autowired
    private DeveloperService developerService;

    @GetMapping("findAll")
    public List<Developer> findAll(){
        List<Developer> developers = developerService.getAll();
        return developers;
    }
    @PostMapping("all") //分页
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
            PageResult result =  developerService.findPage(queryPageBean);
        return result;
    }

//    @PostMapping("add")
    //urlencode方式提交参数
//    public Result addDeveloper(String dname,int [] pids) {
//        int row = developerService.addAndBind(dname, pids);
//        if (row > 0) {
//            return Result.success();
//        } else {
//            return Result.fail();
//        }
//    }
        //json方式提交参数
    @PostMapping("add")
   public Result addDeveloper(@RequestBody DevProVO devProVO){
       int row = developerService.addDeveloper(devProVO);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }

    }


    //用来做修改时的数据回显 json
    @GetMapping("findByDid")  //返回DeveloperDto
    public Result findByDid(@RequestParam("did") String did){
        DeveloperDto developerDto = developerService.findByDid(did);
        Result result = new Result();
        result.setMessage(Status.FAIL.getMessage());
        if (ObjectUtils.isNotEmpty(developerDto)){
            result.setFlag(true);
            result.setData(developerDto);
            result.setMessage(Status.SUCCESS.getMessage());
        }
        return result;
    }


    @PostMapping("edit")
    public Result edit(@RequestBody DeveloperDto developerDto){
        int row  = developerService.edit(developerDto);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @GetMapping("delete")
    public Result delete(@RequestParam("did") String did){
        int row =  developerService.deleteByDid(did);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

}
